package org.example.jedis.queue;

import org.example.jedis.JedisDemo;
import org.junit.Test;
import redis.clients.jedis.ListPosition;

/**
 * @author: tianhao.luo@hand-china.com 2021/5/25  10:48
 */
public class ListQueueControlTest extends JedisDemo {

    /**
     * 命令:lpush key value1 value2 value3 ...
     * 将一个或多个值推入列表的头
     */
    @Test
    public void lpushKeyValues(){
        String key = "topn";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key);
        Long lpush = jedis.lpush(key, value1, value2, value3);
        LOGGER.info("lpush的key:{},执行的结果为:{}",key,lpush);
    }


    /**
     * 命令:lrange key start stop
     * 索引key对应值列表，从start开始，stop结束；0表示头部索引，-1表示尾部索引；注意获取到值列表中的数据顺序，发现获取到的顺序和推入的顺序相反，即最先推入的最后获取
     */
    @Test
    public void lrangeKeyStartStop(){
        String key = "topn";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key);
        Long lpush = jedis.lpush(key, value1, value2, value3);
        LOGGER.info("lpush的key:{},执行的结果为:{}",key,lpush);
        for (String s : jedis.lrange(key, 0, -1)) {
            LOGGER.info("元素为:{}",s);
        }
    }


    /**
     * 命令:rpush key value1 value2 value3...
     * 将一个或多个值推入列表的尾部
     */
    @Test
    public void rpushKeyValues(){
        String key = "topn";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key);
        Long rpush = jedis.rpush(key, value1, value2, value3);
        LOGGER.info("rpush的key:{},执行的结果为:{}",key,rpush);
    }

    /**
     * 命令:lpop key
     * 从列表的头部弹出元素；当列表中的元素被全部弹出后，该key也就消失了。
     */
    @Test
    public void lpopKey(){
        String key = "topn";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key);
        Long lpush = jedis.lpush(key, value1, value2, value3);
        LOGGER.info("lpush的key:{},执行的结果为:{}",key,lpush);
        for (String s : jedis.lrange(key, 0, -1)) {
            LOGGER.info("元素为:{}",s);
        }
        String lpop = jedis.lpop(key);
        LOGGER.info("从key:{}的列表中lpop弹出元素:{}",key,lpop);
    }


    /**
     * 命令:rpop key
     * 从列表的尾部弹出元素；当列表中的元素被全部弹出后，该key也就消失了。
     */
    @Test
    public void rpopKey(){
        String key = "topn";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key);
        Long rpush = jedis.rpush(key, value1, value2, value3);
        LOGGER.info("rpush的key:{},执行的结果为:{}",key,rpush);
        for (String s : jedis.lrange(key, 0, -1)) {
            LOGGER.info("元素为:{}",s);
        }
        String rpop = jedis.rpop(key);
        LOGGER.info("从key:{}的列表中rpop弹出元素:{}",key,rpop);
    }

    /**
     * 命令:rpoplpush key1 key2
     * 从key1对应的列表的尾部弹出一个元素，将该元素从key2对应的列表头部推入。
     */
    @Test
    public void rpoplpushKey1Key2(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);
        Long rpush = jedis.rpush(key1, value1, value2, value3);
        LOGGER.info("rpush的key:{},执行的结果为:{}",key1,rpush);
        String key2 = "k2";
        String value4 = "Hello";
        String value5 = "World";
        String value6 = "Ok";
        delExistsKeyValue(key2);
        Long lpush = jedis.lpush(key2, value4, value5, value6);
        LOGGER.info("lpush的key:{},执行的结果为:{}",key2,lpush);
        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }
        for (String s : jedis.lrange(key2, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key2,s);
        }
        // rpoplpush
        String rpoplpush = jedis.rpoplpush(key1, key2);
        LOGGER.info("rpoplpush的srckey为{},dstkey为{},结果为:{}",key1,key2,rpoplpush);

        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }
        for (String s : jedis.lrange(key2, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key2,s);
        }
    }


    /**
     * 命令:lindex key index
     * 对key对应的值从头部开始索引，获取指定索引处的列表元素
     */
    @Test
    public void lindexKeyIndex(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);
        Long rpush = jedis.rpush(key1, value1, value2, value3);
        LOGGER.info("rpush的key:{},执行的结果为:{}",key1,rpush);

        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }

        int index = 0;
        String lindex = jedis.lindex(key1, index);
        LOGGER.info("lindex的key:{},索引的位置为:{},执行的结果为:{}",key1,index,lindex);
    }


    /**
     * 命令:llen key
     * 获取key对应的值列表的长度
     */
    @Test
    public void llenKey(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);
        Long rpush = jedis.rpush(key1, value1, value2, value3);
        LOGGER.info("rpush的key:{},执行的结果为:{}",key1,rpush);

        Long llen = jedis.llen(key1);
        LOGGER.info("list集合key:{},llen执行的结果为:{}",key1,llen);
    }


    /**
     * 命令:linsert key before|after value newvalue
     * 对key对应的值列表，指定元素value前或者后插入一个新值newvalue
     */
    @Test
    public void linsertKeyBeforeOrAfterValueNewValue(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);
        Long rpush = jedis.rpush(key1, value1, value2, value3);
        LOGGER.info("rpush的key:{},执行的结果为:{}",key1,rpush);

        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }

        String newValue = "Java";
        Long linsert = jedis.linsert(key1, ListPosition.BEFORE, value3, newValue);
        LOGGER.info("linsert的key:{},插入的新值为:{},执行的结果为:{}",key1,newValue,linsert);

        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }
    }


    /**
     * 命令:lrem key count element
     * 对key对应的值列表，从头部开始删除count次element元素,返回的是删除的个数
     */
    @Test
    public void lremKeyCountElement(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);
        Long rpush = jedis.rpush(key1, value1, value1,value2, value3);
        LOGGER.info("rpush的key:{},执行的结果为:{}",key1,rpush);

        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }

        int count = 2;
        Long lrem = jedis.lrem(key1, count, value1);
        LOGGER.info("lrem的key:{},删除的值为:{},次数为:{},执行的结果为:{}",key1,value1,count,lrem);

        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }
    }


    /**
     * 命令:lset key index value
     * 对key对应的值列表的索引index处元素替换为value
     */
    @Test
    public void lsetKeyIndexValue(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);
        Long rpush = jedis.rpush(key1, value1, value2, value3);
        LOGGER.info("rpush的key:{},执行的结果为:{}",key1,rpush);

        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }

        long index = 2;
        String lset = jedis.lset(key1, index, value1);
        LOGGER.info("lset的key:{},替换的新值为:{},索引为:{},执行的结果为:{}",key1,value1,index,lset);

        for (String s : jedis.lrange(key1, 0, -1)) {
            LOGGER.info("key:{}中元素为:{}",key1,s);
        }
    }
}
